﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ploeh.AutoFixture;
using System.IO;
using FileDownloadServer;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.Win32.Dialogs;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UITesting;
using Tests.Properties;
using System.Web.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Linq;
using FluentAssertions;
using ArtOfTest.Common.Win32;
using System.Diagnostics;
using System.Net.Mime;

namespace Tests
{
	[TestClass]
	public class InlinesDownloads
	{
		private const string downloadContentType = "application/download";
		private static readonly TestFile pdfFile = new TestFile(Resources.testpdf, "pdf", new ContentType("application/pdf"), new InlinePdfHtmlDocumentMatcher());
		private const string downloadContentDisposition = "attachment";
		private static readonly Directory applicationPath = new Directory("../../../FileDownloadServer");
		private static Fixture fixture;
		private FileDownloadProperties downloadProperties;
		private string downloadFileName;
		private static FileDownloadTester tester;

		[ClassInitialize]
		public static void SetUpTests(TestContext context)
		{
			fixture = CreateFixture();
			tester = new FileDownloadTester(applicationPath);
		}

		[ClassCleanup]
		public static void DisposeTester()
		{
			tester.Dispose();
		}

		private static Fixture CreateFixture()
		{
			var fixture = new Fixture();

			fixture.Register(() => new FileDownloadProperties
			{
				File = pdfFile.File,
				ResponseHeaders = new ResponseHeaders
				{
					ContentType = pdfFile.ContentType.ToString()
				}
			});

			return fixture;
		}

		[TestInitialize]
		public void SetUp()
		{
			downloadFileName = fixture.Create<string>();
			downloadProperties = fixture.Create<FileDownloadProperties>();
		}

		[TestMethod]
		public void WithDownloadDisposition()
		{
			SetDownloadDisposition();
		}

		private void SetDownloadDisposition()
		{
			SetDisposition(downloadContentDisposition);
		}

		[TestMethod]
		public void ToleratesDispositionWhitespace()
		{
			SetDisposition("     " + downloadContentDisposition + "   ");
		}

		[TestMethod]
		public void WithDownloadContentType()
		{
			SetContentTypeAndExtension(downloadContentType);
		}

		private void SetContentTypeAndExtension(string contentType)
		{
			AddExtensionToUrl();
			SetContentType(contentType);
		}

		[TestMethod]
		public void ToleratesContentTypeWhiteSpace()
		{
			SetContentTypeAndExtension(" " + downloadContentType + "    ");
		}

		private void SetContentType(string value)
		{
			downloadProperties.ResponseHeaders.ContentType = value;
		}

		private void AddExtensionToUrl()
		{
			downloadFileName += "." + pdfFile.Extension;
		}

		private void SetDisposition(string disposition)
		{
			downloadProperties.ResponseHeaders.ContentDisposition = disposition;
		}

		[TestMethod]
		public void WithDownloadDispositionAndNoContentType()
		{
			SetDownloadDisposition();
			SetContentTypeAndExtension(null);
		}

		[TestCleanup]
		public void Test()
		{
			var result = tester.Test(downloadFileName, downloadProperties, pdfFile.Matcher).Result;
			result.Should().Be(TestResult.Displayed);
		}
	}
}
